const app = getApp();
var util = require('../../utils/util.js');
var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
    data: {
        mapkey:app.globalData.qqmapkey,
        location:{
            latitude: 34.74821,
            longitude: 113.61332,
            scale:14
        },
        equipmentAreaList:[],
        markers: [],
        src:'',
        inputVal: "",
        inputShowed: false,
    },
    onLoad: function (option) {
        this.getLocation();
        this.getAreaList();
        var that = this;
        qqmapsdk = new QQMapWX({
            key: that.data.mapkey,
        });
        wx.setNavigationBarTitle({
            title: '设备区域'
        })

    },
    getLocation (){
        var that = this;
        wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success(res) {
                console.log(res)
                that.setData({
                    location:{
                        latitude:res.latitude,
                        longitude:res.longitude
                    }
                })
            }
        })
    },
    regionchange(e) {
        console.log(e.type)
    },
    markertap(e) {
        console.log(e.markerId)
    },
    locationPosition(e) {
        var mapCtx = wx.createMapContext('map');
        mapCtx.moveToLocation();
    },
    getAreaList (name){
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        if(!name){
            name =''
        }
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/getEquipAreaList',
            data: {
                token: app.globalData.token,
                name:name
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res)
                if(app.requestEro(res.data.success,res.data.msg)){
                    var markers = res.data.equipAreaList.map(function (item) {
                        var Obj={};
                        Obj.id = item.id;
                        Obj.iconPath = '/images/maps/marker.png';
                        Obj.width = 22;
                        Obj.height= 35;
                        Obj.latitude = item.latitude;
                        Obj.longitude = item.longitude;
                        Obj.callout = {
                            content:item.name,
                            display:'ALWAYS',
                            fontSize:14,
                            padding:5
                        };
                        // Obj.label = {
                        //     content: item.num +'台',
                        //     color: '#333',
                        //     bgColor: '#FFFFFF',
                        //     anchorX: 600,
                        //     anchorY: 52,
                        //     padding:10
                        // }
                        return Obj;

                    })
                    console.log(markers)
                    that.setData({
                        markers:markers,
                        equipmentAreaList:res.data.equipAreaList
                    })
                }

            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false
        });
        this.getAreaList()
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value,
        })
        this.getAreaList(this.data.inputVal)

    },
    getEquipmentList(e) {
        console.log(e);
        let marker = this.data.markers.filter(function (item) {
            return item.id === e.markerId
        })
        console.log(marker);
        wx.navigateTo({
            url:"/pages/areas-equipment/areas-equipment?id="+marker[0].id+"&name="+marker[0].callout.content
        })

    }
})